﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Rung.Soft.Jx3Tookit.Addon.AutoPress.Business;
using Rung.Soft.Jx3Tookit.Addon.AutoPress.Utility;
using Rung.Soft.Jx3Tookit.Business;
using System.Reflection;

namespace Rung.Soft.Jx3Tookit.Addon.AutoPressAddon
{
    public partial class ConfigForm : Form
    {
        public const string JOBS_FILE_NAME = "jobs.dat";
        public static readonly Dictionary<string, SkillJober> sJobers;
        private SkillJober currJober;
        private MainUC mainUC;

        static ConfigForm()
        {
            if (File.Exists(JOBS_FILE_NAME))
            {
                try
                {
                    string fileFullName = StaticFunction.GetUserFileFullName(Addon._Name, JOBS_FILE_NAME);
                    sJobers = SerializeHelper.DeSerialize<Dictionary<string, SkillJober>>(fileFullName, new AssemblyBinder());
                    //sJobers = SerializeHelper.DeSerialize<Dictionary<string, SkillJober>>(fileFullName);
                }
                catch(Exception e)
                { MessageBox.Show(string.Format("载入现有方案异常：{0}。", e.Message)); }
            }

            if (sJobers == null)
                sJobers = new Dictionary<string, SkillJober>();
        }

        private ConfigForm()
        {
            InitializeComponent();
        }

        public ConfigForm(SkillJober skillJober, MainUC mainUC)
            : this()
        {
            this.currJober = skillJober;
            this.mainUC = mainUC;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.currJober.Name = txbName.Text.Trim();
            if (sJobers.ContainsKey(this.currJober.Name))
                MessageBox.Show(string.Format("已存在名为“{0}”的方案。", this.currJober.Name));
            else
            {
                sJobers.Add(this.currJober.Name, this.currJober);
            }

            if (StaticFunction.IsUserFileExist(Addon._Name, JOBS_FILE_NAME))
            {
                //MessageBox.Show("该方案名文件已存在。");
                StaticFunction.DeleteUserFile(Addon._Name, JOBS_FILE_NAME);
            }
            //else
            {
                string fileFullName = StaticFunction.GetUserFileFullName(Addon._Name, JOBS_FILE_NAME);
                SerializeHelper.Serialize(sJobers, fileFullName);
                MessageBox.Show("保存成功。");
            }

            mainUC.tsm载入已存方案_Refresh();

            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        internal class AssemblyBinder : System.Runtime.Serialization.SerializationBinder
        {
            public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
            {
                base.BindToName(serializedType, out assemblyName, out typeName);
            }

            public override Type BindToType(string assemblyName, string typeName)
            {
                Assembly a = Assembly.GetExecutingAssembly();
                ////Assembly.LoadFile(Assembly.GetExecutingAssembly().Location);
                ////Module[] m = a.GetModules(false);
                //////return a.GetType(typeName);
                ////AssemblyName[] an = a.GetReferencedAssemblies();
                Type t = a.GetType(typeName);

                //if (typeName == "System.Windows.Forms.Keys")
                //    t = typeof(System.Windows.Forms.Keys);
                //else if (typeName == "System.Collections.Generic.List`1[[Rung.Soft.Jx3Tookit.Addon.AutoPress.Business.SkillAction, AutoPressAddon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]")
                //    t = typeof(List<SkillAction>);
                //else if (typeName == "System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Rung.Soft.Jx3Tookit.Addon.AutoPress.Business.SkillJober, AutoPressAddon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]")
                //    t = typeof(Dictionary<string, SkillJober>);

                return t;
            }
        } 
    }
}
